package com.example.bes.springboot.jdbc.war.controller;

import com.example.bes.springboot.jdbc.war.mapper.Obd1Mapper;
import com.example.bes.springboot.jdbc.war.mapper.UserMapper;
import com.example.bes.springboot.jdbc.war.model.Obd1;
import com.example.bes.springboot.jdbc.war.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping(value = "/jdbc")
public class TestController {

    private static final Logger log = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private Obd1Mapper obd1Mapper;

    @Autowired
    private DataSource dataSource;

    @GetMapping(value = "/mysql/user/all")
    public List<User> selectAll() {
        return userMapper.selectUserList();
    }

    @GetMapping(value = "/oracle/user/all")
    public List<Obd1> selectObd1All() {
        return obd1Mapper.selectobd();
    }

    @GetMapping(value = "/slowdown")
    public String slowdown() {
        long slowdown = userMapper.slowdown();
        return "success" + slowdown;
    }

    @GetMapping(value = "/geturl")
    public String getAllConnection() throws SQLException {
        //for (int i = 0; i < 8; i++) {
        //new Thread(()->{
        try {
            Connection connection = dataSource.getConnection();
            System.out.println("====================");
            String ourl = connection.getMetaData().getURL();
            System.out.println("connection url: " + ourl);
            System.out.println("====================");
            /*Statement stmt = connection.createStatement();
            ResultSet resultSet1 = stmt.executeQuery("SELECT sys_context('USERENV', 'IP_ADDRESS') FROM DUAL");
            if (resultSet1.next()) {
                System.out.println("Database ip: " + resultSet1.getString(1));
            }
            stmt.close();
            resultSet1.close();*/
            connection.close();
            return ourl;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        //}).start();
        //}
    }
}
